//
//  SmsCloudVipController.m
//  smsCloud
//
//  Created by 吴斌 on 2024/7/22.
//

#import "SmsCloudVipController.h"
#import "SmsCloudWebController.h"
@interface SmsCloudVipController ()
@property (nonatomic, strong)UILabel * smsCloudVipPriceLab;
@end

@implementation SmsCloudVipController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#FFFFFF"];
    [self smsCloudInitView];
}
- (void)smsCloudInitView{
    UIImageView* smsCloudVipBgImgView = [[UIImageView alloc] init];
    smsCloudVipBgImgView.image = [SmsCloudHead SmsCloudImageNamed:@"smsCloudVipBg"];
    [self.view addSubview:smsCloudVipBgImgView];
    [smsCloudVipBgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
    }];
    UIButton * smsCloudVipCloseBtn = [[UIButton alloc] init];
    [smsCloudVipCloseBtn addTarget:self action:@selector(smsCloudVipClose) forControlEvents:UIControlEventTouchUpInside];
    [smsCloudVipCloseBtn setBackgroundImage:[SmsCloudHead SmsCloudImageNamed:@"smsCloudVipClose"] forState:UIControlStateNormal];
    [self.view addSubview:smsCloudVipCloseBtn];
    [smsCloudVipCloseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:12] + [SmsCloudHead SmsCloudTopSafeH]));
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:64]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:64]));
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:24]));
    }];
    
    UIButton * vipPriAgreementBtn = [[UIButton alloc] init];
    [vipPriAgreementBtn addTarget:self action:@selector(vipPriAgreement) forControlEvents:UIControlEventTouchUpInside];
    [vipPriAgreementBtn setTitle:@"隐私协议" forState:UIControlStateNormal];
    [vipPriAgreementBtn setTitleColor:[SmsCloudHead SmsCloudColorHexString:@"#01F2E7"] forState:UIControlStateNormal];
    vipPriAgreementBtn.titleLabel.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:24] weight:UIFontWeightRegular];
    [self.view addSubview:vipPriAgreementBtn];
    [vipPriAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:125]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:40]));
        make.bottom.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:127]));
    }];
    UIButton * vipUserAgreementBtn = [[UIButton alloc] init];
    [vipUserAgreementBtn addTarget:self action:@selector(vipUserAgreement) forControlEvents:UIControlEventTouchUpInside];
    [vipUserAgreementBtn setTitle:@"用户协议" forState:UIControlStateNormal];
    [vipUserAgreementBtn setTitleColor:[SmsCloudHead SmsCloudColorHexString:@"#01F2E7"] forState:UIControlStateNormal];
    vipUserAgreementBtn.titleLabel.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:24] weight:UIFontWeightRegular];
    [self.view addSubview:vipUserAgreementBtn];
    [vipUserAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(vipPriAgreementBtn.mas_left);
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:125]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:40]));
        make.top.equalTo(vipPriAgreementBtn);
    }];
    UIButton * vipUserBuyBtn = [[UIButton alloc] init];
    vipUserBuyBtn.tag = 101;
    [vipUserBuyBtn addTarget:self action:@selector(vipUserBuy:) forControlEvents:UIControlEventTouchUpInside];
    [vipUserBuyBtn setTitle:@"恢复购买" forState:UIControlStateNormal];
    [vipUserBuyBtn setTitleColor:[SmsCloudHead SmsCloudColorHexString:@"#01F2E7"] forState:UIControlStateNormal];
    vipUserBuyBtn.titleLabel.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:24] weight:UIFontWeightRegular];
    [self.view addSubview:vipUserBuyBtn];
    [vipUserBuyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(vipPriAgreementBtn.mas_right);
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:125]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:40]));
        make.top.equalTo(vipPriAgreementBtn);
    }];
    UIButton * vipBuyBtn = [[UIButton alloc] init];
    vipBuyBtn.tag = 100;
    [vipBuyBtn setBackgroundImage:[SmsCloudHead SmsCloudImageNamed:@"smsCloudBuy"] forState:UIControlStateNormal];
    [vipBuyBtn addTarget:self action:@selector(vipUserBuy:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:vipBuyBtn];
    [vipBuyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:598]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:86]));
        make.bottom.equalTo(vipUserBuyBtn.mas_top).offset(-[SmsCloudHead SmsCloudScreewRatio:32]);
    }];
    UIImageView* smsCloudVipValueImgView = [[UIImageView alloc] init];
    smsCloudVipValueImgView.image = [SmsCloudHead SmsCloudImageNamed:@"smsCloudVipCenter"];
    [self.view addSubview:smsCloudVipValueImgView];
    [smsCloudVipValueImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:440]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:228]));
        make.bottom.equalTo(vipBuyBtn.mas_top).offset(-[SmsCloudHead SmsCloudScreewRatio:96]);
    }];
    self.smsCloudVipPriceLab = [[UILabel alloc] init];
    self.smsCloudVipPriceLab.numberOfLines = 0;
    self.smsCloudVipPriceLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.smsCloudVipPriceLab];
    [self.smsCloudVipPriceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:66]));
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:66]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:100]));
        make.bottom.equalTo(smsCloudVipValueImgView.mas_top).offset(-[SmsCloudHead SmsCloudScreewRatio:73]);
    }];
    NSString * smsCloudVipPrice = [NSString stringWithFormat:@"免费试用3天，可自动续订，续费￥%@/年，免费享受后续功能迭代，无需额外付费，随时取消",[SmsCloudDataHelper defaultMag].smsCloudPrice];
    NSString *smsCloudVipChange = [NSString stringWithFormat:@"续费￥%@/年",[SmsCloudDataHelper defaultMag].smsCloudPrice];
    NSRange priceRange = [smsCloudVipPrice rangeOfString:smsCloudVipChange];
    
    NSMutableAttributedString *smsCloudVipMutPrice = [[NSMutableAttributedString alloc] initWithString:smsCloudVipPrice];
    [smsCloudVipMutPrice addAttribute:NSForegroundColorAttributeName value:[SmsCloudHead SmsCloudColorHexString:@"#FFFFFF"] range:NSMakeRange(0,smsCloudVipMutPrice.length)];
    [smsCloudVipMutPrice addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:28] weight:UIFontWeightRegular] range:NSMakeRange(0,smsCloudVipMutPrice.length)];
    [smsCloudVipMutPrice addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:32] weight:UIFontWeightBold] range:priceRange];
    self.smsCloudVipPriceLab.attributedText = smsCloudVipMutPrice;
    
    UILabel *smsCloudVipPriceHeadLab = [[UILabel alloc] init];
    smsCloudVipPriceHeadLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#FFFFFF"];
    smsCloudVipPriceHeadLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:50] weight:UIFontWeightRegular];
    smsCloudVipPriceHeadLab.text = @"短信云";
    [self.view addSubview:smsCloudVipPriceHeadLab];
    [smsCloudVipPriceHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:60]));
        make.bottom.equalTo(self.smsCloudVipPriceLab.mas_top).offset(-[SmsCloudHead SmsCloudScreewRatio:14]);
    }];
}
- (void)smsCloudVipClose{
    [self dismissViewControllerAnimated:true completion:^{
            
    }];
}
- (void)vipPriAgreement{
    SmsCloudWebController * smsCloudWebCtrl = [[SmsCloudWebController alloc] init];
    smsCloudWebCtrl.smsCloudWebUrl = [SmsCloudHead SmsCloudPublicForUserAg];
    [self.navigationController pushViewController:smsCloudWebCtrl animated:true];
}
- (void)vipUserAgreement{
    SmsCloudWebController * smsCloudWebCtrl = [[SmsCloudWebController alloc] init];
    smsCloudWebCtrl.smsCloudWebUrl = [SmsCloudHead SmsCloudPublicForPriAg];
    [self.navigationController pushViewController:smsCloudWebCtrl animated:true];
}
- (void)vipUserBuy:(UIButton *)sender{
    __weak __typeof__(self) weakSelf = self;
    [[SmsCloudApplePay defaultRequest] applePayProId:[SmsCloudHead SmsCloudPublicAppProductId]];
    [SmsCloudApplePay defaultRequest].ApplePayScuBlock = ^{
        NSDate *curDate = [NSDate date];
        NSCalendar *curCalendar = [NSCalendar currentCalendar];
        NSDateComponents *curDateComponent = [[NSDateComponents alloc] init];
        [curDateComponent setYear:1];
        NSDate *addDate = [curCalendar dateByAddingComponents:curDateComponent toDate:curDate options:0];
        NSTimeInterval curTime = [addDate timeIntervalSince1970];
        [[NSUserDefaults standardUserDefaults] setDouble:(double)curTime forKey:@"smsCloudVipBuyTime"];
        if (sender.tag == 100) {
            [SmsCloudDataHelper requestSmsCloudVipMd];
        }
        if(weakSelf.ApplePayScuBlock){
            weakSelf.ApplePayScuBlock();
        }
        [weakSelf dismissViewControllerAnimated:true completion:^{}];
    };
}
@end
